package org.huyue.huyuexia.mapper;

import org.apache.ibatis.annotations.*;
import org.huyue.huyuexia.pojo.User;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {
    /**
     * 查询所有用户
     * @return
     */
    @Select("SELECT * FROM users")
    List<User> findAll();

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Select("SELECT * FROM users WHERE id = #{id}")
    User findById(Integer id);

    /**
     * 删除用户
     * @param id
     */
    @Delete("DELETE FROM users WHERE id = #{id}")
    void Delete(Integer id);

    @Insert("insert into users(username, password_hash, nickname, follower_count, following_count, bean_balance, avatar_url, bio, wechat_qrcode_url, wechat_openid, status, create_time, update_time)"+
            "values (#{username},#{passwordHash},#{nickname},#{followerCount},#{followingCount},#{beanBalance},#{avatarUrl},#{bio},#{wechatQrcodeUrl},#{wechatOpenid},#{status},#{createTime},#{updateTime})")
    void add(User user);

    @Update("update users set username=#{username},password_hash=#{passwordHash},nickname=#{nickname},follower_count=#{followerCount},following_count=#{followingCount},bean_balance=#{beanBalance},avatar_url=#{avatarUrl},bio=#{bio},wechat_qrcode_url=#{wechatQrcodeUrl},wechat_openid=#{wechatOpenid},status=#{status},create_time=#{createTime},update_time=#{updateTime} where id=#{id}")
    void Update(User user);

    //根据用户名和密码查询是否有匹配的
    @Select("SELECT * FROM users WHERE username = #{username} AND password_hash = #{passwordHash}")
    User findByUsernameAndPassword(String username, String passwordHash);

    // 增加阅豆
    @Update("update users set bean_balance=bean_balance+ #{beanBalance} where id=#{id}")
    void updateBigDecimal (Integer id, Integer beanBalance);

    // 统计前十的每个用户发文章数量
    @Select("select u.nickname,count(a.id) from article as a left join users as u on a.author_id = u.id group by u.nickname")
    List<Map> countArticle();
}
